package com.yoomiito.app.ui.my.remain.submit;

import android.content.res.Configuration;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import com.jakewharton.rxbinding3.widget.RxTextView;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.model.my.MyServicePrecent;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.inputfilter.CashierInputFilter;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.widget.EditTextAndDel;
import com.yoomiito.app.widget.PasswordView_1;
import com.yoomiito.app.widget.TipDialog;
import com.yoomiito.app.widget.dialog.PswInputDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class RemainFragment extends BaseLazyFragment<RemainPresenter> {
    @BindView(R.id.et_money)
    EditTextAndDel mEditTextAndDel;
    @BindView(R.id.tv_tip)
    TextView mTipTv;
    @BindView(R.id.submit_all)
    TextView mSubmitAllTv;
    @BindView(R.id.btn_submit)
    Button mButton;

    private String mRemainMoney;//余额
    private int mSubmitMaxMoney = 5000;//最大限额
    private String submitMoney;//提现金额
    private PswInputDialog pswInputDialog;

    @Override
    public int getLayoutId() {
        return R.layout.fm_remain;
    }

    @Override
    public RemainPresenter newP() {
        return new RemainPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mEditTextAndDel.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        mEditTextAndDel.setEditFilters(new InputFilter[]{new CashierInputFilter()});
        mEditTextAndDel.setCursorColor(R.drawable.cursor);
        mEditTextAndDel.setTypeface(AllUtils.getTypeface());
        mEditTextAndDel.setIvWidth(DensityUtil.dp2px(16), DensityUtil.dp2px(16));
        mEditTextAndDel.getEditText().setRawInputType(Configuration.KEYBOARD_QWERTY);

        RxTextView.textChanges(mEditTextAndDel.getEditText())
//                .delay(800, TimeUnit.MILLISECONDS)
//                .filter(charSequence ->
//                         !TextUtils.isEmpty(charSequence.toString())
//                )
//                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(charSequence -> {
                    if (TextUtils.isEmpty(charSequence.toString())) {
                        setRemainMoney(mRemainMoney);
                        mButton.setEnabled(false);
                        return;
                    }
//                    Double inputMoney = Double.parseDouble(charSequence.toString());
                    String money = charSequence.toString();
                    if (!AllUtils.moneyCompare(money, "2")) {
                        setTip("提现金额必须大于2元");
                        mButton.setEnabled(false);
                        return;
                    }
                    if (!AllUtils.moneyCompare(mRemainMoney,money)){
                        setTip("提现金额超过可提现余额");
                        mButton.setEnabled(false);
                        return;
                    }
                    if (!AllUtils.moneyCompare(String.valueOf(mSubmitMaxMoney), money)) {
                        setTip("微信零钱提现每日每次限额¥" + mSubmitMaxMoney);
                        mButton.setEnabled(false);
                        return;
                    }
                    setRemainMoney(mRemainMoney);
                    mButton.setEnabled(true);
                });
        getP().getIsAgent();
    }

    @OnClick({R.id.submit_all, R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit_all://全部提现
                mEditTextAndDel.getEditText().setText(mRemainMoney);
                break;
            case R.id.btn_submit://提现
                submitMoney = mEditTextAndDel.getEditText().getText().toString().trim();
                getP().getWithDrawServicePresent(submitMoney);
                break;
        }
    }

    /**
     * 设置余额
     */
    public void setRemain(MyRemainMoneyInfo data) {
        setRemainMoney(data.getMoney());
    }

    //设置金额
    public void setRemainMoney(String money) {
        dismissLoading();
        if (View.VISIBLE != mSubmitAllTv.getVisibility()) {
            mSubmitAllTv.setVisibility(View.VISIBLE);
        }
        mRemainMoney = money;
        mTipTv.setText("可提现余额 ¥" + money + "，");
        mTipTv.setTextColor(ResUtil.getColor(R.color.color_balk_444444));
    }

    public void setTip(String tip) {
        mSubmitAllTv.setVisibility(View.GONE);
        mTipTv.setTextColor(ResUtil.getColor(R.color.color_FA0C07));
        mTipTv.setText(tip);
    }

    //显示运营管理费弹窗  优米用户不显示该弹窗
    public void showSureDialog(MyServicePrecent data) {
        if (SpannableUtils.isEmptyOr0(data.getPrecent())) {//优米用户不显示该弹窗
            toSubmit();
        } else {
            String content = "提现需收取平台运营管理费用" + data.getPrecent()+"%" + "，本次实际到账金额" + data.getLeft() + "元";
            showSelectDialog(ResUtil.getString(R.string.my_tip),
                    content,
                    ResUtil.getString(R.string.my_sure_get),
                    "取消",
                    new TipDialog.Dialogcallback() {

                        @Override
                        public void btnConfirm() {
                            toSubmit();
                        }

                        @Override
                        public boolean cancel() {
                            return false;
                        }
                    });
        }
    }



    public void toSubmit() {
        if (pswInputDialog == null) {
            pswInputDialog = new PswInputDialog(context);
            pswInputDialog.setTextChangedListener(new PasswordView_1.TextChangedListener() {
                @Override
                public void textChanged(CharSequence changeText) {

                }

                @Override
                public void textCompleted(CharSequence text) {
                    showLoading();
                    getP().submitData(submitMoney,text.toString());
                }
            });
        }
        pswInputDialog.show();
    }

    public void error(boolean pswError,String tip) {
        pswInputDialog.showErrorTip(pswError,tip);
        if (!pswError) {
            pswInputDialog.dismiss();
            getP().getIsAgent();
        }
    }

    public void rsaPswError() {
        dismissLoading();
        context.finish();
    }


    public void submitSuccess() {
        dismissLoading();
        pswInputDialog.dismiss();
        ((RemainActivity) context).switchFm(1);
    }
}
